package d4_tcp01;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {
    public static void main(String[] args) throws Exception {
        System.out.println("==服务端启动==");
        //目标: 掌握TCP通信服务端的开发
        //1、注册端口
        ServerSocket serverSocket = new ServerSocket(8848);

        //2、开始等待客户端的Socket管道连接
        Socket socket = serverSocket.accept();
        //3、从socket通信管道中得到一个字节输入流
        InputStream is = socket.getInputStream();
        //4、把低级的字节输入流包装成高级的特殊数据输入流
        DataInputStream dis = new DataInputStream(is);
        //5、接收数据
        int msg = dis.readInt();
        String msg1 = dis.readUTF();

        //6、拿对方的IP和端口
        System.out.println("对方IP: " + socket.getInetAddress().getHostAddress());
        System.out.println("对方端口: " + socket.getPort());
        //进行信息的展示
        System.out.println("服务端收到: " + msg + "\r\n" + msg1);

        socket.close();

    }
}
